import axios from 'axios';
import type { InternalAxiosRequestConfig, AxiosResponse } from 'axios';
import { useUserStore } from '@/stores/user';
// 创建 axios 实例
const service = axios.create({
   // baseURL: 'https://scm.impulsefitness.com/wx_back/',//http://172.16.71.26:7792/api/otd
    // baseURL: 'http://172.16.71.26:7792/',//http://172.16.71.26:7792/api/otd
    baseURL:'http://localhost:26437/',
    timeout: 50000,
    headers: { 'Content-Type': 'application/json;charset=utf-8' }
  });

  
// 请求拦截器
service.interceptors.request.use(
    (config: InternalAxiosRequestConfig) => {
      // const userStore = useUserStore().user
      // if (userStore?.openid) {
      //   config.headers.set('openid',userStore.openid);
      // }
      return config;
    },
    (error: any) => {
      return Promise.reject(error);
    }
  );
  // 响应拦截器
  service.interceptors.response.use(
    async (response: AxiosResponse) => {
      // return response;
      const { code, msg } = response.data;
      if (code === '000000') {
        return response.data;
      }
      if (response.data instanceof ArrayBuffer) {
        return response
      }
      return Promise.reject(new Error(msg || 'Error'))
    },
    (error) => {
      // return Promise.reject(error.message)
      // console.log(error)
      if (error.code == "ERR_NETWORK") {
        Promise.reject(error.message || '网络发生问题')
      } else {
        return Promise.reject(error.message)
      }      
    }
  );
  
  export default service;